//
//  HomeExhibitCollectionCell.m
//  ArtDog
//
//  Created by dllo on 16/9/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "HomeExhibitCollectionCell.h"
#import "BaseTableView.h"
#import "HLMacro.h"
#import "ZWYNetTool.h"
#import "NSString+Common.h"
#import "ExhibitTableViewCell.h"
#import "MJRefresh.h"
#import "MJRefreshComponent+RefreshStyle.h"
#import "GalleriesTableViewCell.h"
#import "ExhibitDetailsViewController.h"
#import "UIView+Common.h"

#import "GalleryExhibits.h"
#import "GalleryVos.h"
#import "GalleriesMainModel.h"
#import "ExhibitMainModel.h"
#import "ExhibitArtist.h"

@interface HomeExhibitCollectionCell ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet BaseTableView *tableView;

@property (nonatomic, assign) NSInteger pageIndex;

@property (nonatomic, strong) GalleriesMainModel  *mainModel;
@property (nonatomic, strong) NSMutableArray<NSMutableArray *> *loadArray;

@end

@implementation HomeExhibitCollectionCell

- (void)awakeFromNib {
    [super awakeFromNib];
    if (_mainModel == nil) {
        _mainModel = [[GalleriesMainModel alloc]init];
    }
    if (_loadArray == nil) {
        NSMutableArray *topicList = [NSMutableArray array];
        _loadArray = [NSMutableArray arrayWithObjects:topicList, topicList, nil];
    }
    [self setupBasic];
}

- (void)setupBasic
{
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_tableView registerNib:[UINib nibWithNibName:@"ExhibitTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"exhibitTableViewCell"];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tableViewCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"GalleriesTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"galleriesTableViewCell"];
}

- (void)setType:(NSInteger)type
{
    
    _type = type;
    [_tableView reloadData];
    if (_tableView.mj_header == nil) {
        [self refreshTableView];
    }
    if (_loadArray[type].count == 0) {
        [self handleData];
    }
    
}

#pragma mark - 数据请求
- (void)handleData
{
    _pageIndex = 1;
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _loadArray[_type] = [NSMutableArray array];
        if (_type == 0) {
            for (NSDictionary *dic in result[@"data"]) {
                ExhibitMainModel *exhibitModel = [ExhibitMainModel modelWithDic:dic];
                [_loadArray[_type] addObject:exhibitModel];
            }
        } else {
            _mainModel = [GalleriesMainModel modelWithDic:result[@"data"]];
            for (GalleryVos *model in _mainModel.allGalleryVos) {
                [_loadArray[_type] addObject:model];
            }
        }
        [_tableView.mj_header endRefreshing];
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_tableView.mj_header endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (void)handleLoadData
{
    NSString *urlString = [self getUrlString];
    [ZWYNetTool GET:urlString andBody:nil andHeader:HOSTHEADER andResponse:ZWYJSON andSuccessBlock:^(id result) {
        if (_type == 0) {
            for (NSDictionary *dic in result[@"data"]) {
                ExhibitMainModel *exhibitModel = [ExhibitMainModel modelWithDic:dic];
                [_loadArray[_type] addObject:exhibitModel];
            }
        } else {
            GalleriesMainModel *mainModel = [GalleriesMainModel modelWithDic:result[@"data"]];
            for (GalleryVos *model in mainModel.allGalleryVos) {
                [_loadArray[_type] addObject:model];
            }
        }
        [_tableView.mj_footer endRefreshing];
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        [_tableView.mj_footer endRefreshing];
        NSLog(@"%@", error);
    }];
}

- (NSString *)getUrlString
{
    NSString *urlString;
    if (_type == 0) {
        urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"v3/disExhibitsAndInit"] andParams:@{@"pageIndex": [NSString stringWithFormat:@"%ld", _pageIndex], @"token": [NSString getUserDefaultToken], @"isRecommend": @"0"}];
    } else {
        urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"v3/discoveryGalleries"] andParams:@{@"pageIndex": [NSString stringWithFormat:@"%ld", _pageIndex], @"token": [NSString getUserDefaultToken]}];
    }
    return urlString;
}

#pragma mark - tableView 协议方法

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (_type == 0) {
        return 1;
    } else {
        return 2;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_type == 0) {
        return _loadArray[_type].count;
    } else {
        if (section == 0) {
            return _mainModel.recommendGalleryVos.count + 1;
        } else {
            return _loadArray[_type].count + 1;
        }
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_type == 0) {
        ExhibitTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"exhibitTableViewCell"];
        cell.exhibitModel = _loadArray[_type][indexPath.row];
        return cell;
    } else {
        if (indexPath.row == 0) {
            UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell"];
            cell.imageView.backgroundColor = [UIColor blackColor];
            cell.imageView.image = [UIImage imageNamed:@"leftImage"];
            cell.textLabel.text = indexPath.section == 0 ? @"推荐机构": @"全部机构";
            cell.textLabel.font = [UIFont boldSystemFontOfSize:15];
            return cell;
        } else {
            GalleriesTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"galleriesTableViewCell"];
            if (indexPath.section == 0) {
                cell.galleryModel = _mainModel.recommendGalleryVos[indexPath.row - 1];
            } else {
                cell.galleryModel = _loadArray[_type][indexPath.row - 1];
            }
            return cell;
        }
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_type == 0) {
        return 115 + SCREEN_WIDTH / 3;
    } else if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            return 50;
        } else {
            return 100 + SCREEN_WIDTH / 3;
        }
    } else {
        if (indexPath.row == 0) {
            return 50;
        } else {
            return 80;
        }
    }
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_type == 0) {
        ExhibitDetailsViewController *detailsVC = [[ExhibitDetailsViewController alloc]init];
        UIViewController *vc = [self getVCRecently];
        detailsVC.urlString = [NSString getURLStringWith:HOST andPaths:@[@"artgoer/api/v1/user", [NSString getUserDefaultId], @"exhibit", [NSString stringWithFormat:@"%@", [[_loadArray[_type][indexPath.item]exhibitArtistList][0] idField]]] andParams:@{@"token": [NSString getUserDefaultToken]}];
        [vc.navigationController pushViewController:detailsVC animated:YES];
    }
}


#pragma mark - 刷新
- (void)refreshTableView
{
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self handleData];
    }];
    [_tableView.mj_header mjRefreshStateHeaderStyle];
    
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        if (_loadArray[_type].count % 10 == 0) {
            _pageIndex = _loadArray[_type].count / 10 + 1;
        } else {
            _pageIndex = _loadArray[_type].count / 10 + 2;
        }
        [self handleLoadData];
    }];
    [_tableView.mj_footer mjRefreshAutoStateFooterStyle];
}


@end
